﻿#region Imported Libraries
using System;
using org.webguvenligi.NSecureImageLib.GraphicFileFormats.Utilities;
using System.IO;
#endregion
namespace org.webguvenligi.NSecureImageLib.GraphicFileFormats.GIF89A
{
    /// <summary>
    /// General Purpose GifParser For NSecureImage Library
    /// </summary>
    /// <remarks>
    ///    The Grammar.
    /// <GIF Data Stream> ::=     Header <Logical Screen> <Data>* Trailer
    /// <Logical Screen> ::=      Logical Screen Descriptor [Global Color Table]
    /// <Data> ::=                <Graphic Block>  | <Special-Purpose Block>
    /// <Graphic Block> ::=       [Graphic Control Extension] <Graphic-Rendering Block>
    /// <Graphic-Rendering Block> ::=  <Table-Based Image>  | Plain Text Extension
    /// <Table-Based Image> ::=   Image Descriptor [Local Color Table] Image Data
    /// <Special-Purpose Block> ::=    Application Extension  | Comment Extension                               
    /// </remarks>
    public sealed class GifParser : BaseImageParser
    {
        public GifParser(Stream bitmapStream)
            : base(bitmapStream){
        }
        private static byte[] GifHeader = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 };
        private static byte[] GifHeader1 = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 };
        protected override bool ParseImage()
        {
            
            if (this.ImageStream.Length < GifHeader.Length)
                return false;
            if (this.ImageStream.CompareByteArrayPeeking(GifHeader)
                || this.ImageStream.CompareByteArrayPeeking(GifHeader1))
            {
                this.ImageStream.Position += GifHeader.Length;
            }
            else
            {
                return false;
            }
            return true;
        }

        protected override string FormatExceptionMessage
        {
            get { return "Not a valid Gif File"; }
        }

        protected override byte[] HeaderSignature
        {
            get { throw new NotImplementedException(); }
        }

        public override void WriteImage(Stream targetStream, bool removeMetadataTags)
        {
            throw new NotImplementedException();
        }
    }
}
